package com.sunrin.applepie.gm_player.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;

public class Tab_AlbumActivity extends ListActivity {

	SimpleCursorAdapter adapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		String[] from = { MediaStore.MediaColumns.TITLE };
		int[] to = { android.R.id.text1 };

		Cursor cursor = managedQuery(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Audio.Media.ALBUM);

		adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_1, cursor, from, to);
		setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Cursor cursor = adapter.getCursor();
		cursor.moveToPosition(position);

		String _id = cursor.getString(cursor
				.getColumnIndex(MediaStore.Audio.Media._ID));
		String title = cursor.getString(cursor
				.getColumnIndex(MediaStore.Audio.Media.TITLE));
		String artist = cursor.getString(cursor
				.getColumnIndex(MediaStore.Audio.Media.ARTIST));
		String album = cursor.getString(cursor
				.getColumnIndex(MediaStore.Audio.Media.ALBUM));
		String path = cursor.getString(cursor
				.getColumnIndex(MediaStore.Audio.Media.DATA));
		int duration = cursor.getInt(cursor
				.getColumnIndex(MediaStore.Audio.Media.DURATION));

		String info = "_ID: " + _id + "\n" + "path: " + path + "\n" + "TITLE: "
				+ title + "\n" + "ARTIST: " + artist + "\n" + "ALBUM: " + album
				+ "\n" + "DURATION: " + duration / 1000 + "s";

		Toast.makeText(this, info, Toast.LENGTH_LONG).show();
		
		Intent i = new Intent(Tab_AlbumActivity.this, MusicPlayActivity.class);
		i.putExtra("path", path);
		startActivity(i);
		
	}
}